import { defineStore } from 'pinia'
import { userKey } from './keys'
interface IUserState {
  menus: IMenu[],
  username: string
}
type menu = IMenu & { children: IMenu[] };
export const useUserStore = defineStore(userKey, {
  state(): IUserState {
    return {
      menus: [],
      username: "admin"
    }
  },
  getters: {
    routes() {
      let menus: menu[] = [];
      // 找到所有一级
      menus = this.menus
        .filter((item) => item.parentId == 0)
        .map((item) => ({
          ...item,
          children: [],
        }));
      //   继续把二级加入对于的1级的children里面
      this.menus.forEach((sub) => {
        const p = menus.find((item) => item.id === sub.parentId);
        //   如果找到我的上级，加入它的children
        if (p) {
          p.children.push(sub);
        }
      });

      return menus;
    },
    Permissions(): string[] {
      return this.menus.map(item => item.name)
    }
  }
})
